package models
{
	import com.pamakids.models.ResultVO;

	import flash.events.EventDispatcher;

	import mx.collections.ArrayCollection;

	import controllers.API;

	public class AlbumManager extends EventDispatcher
	{
		public function AlbumManager()
		{
		}

		private static const PAGESIZE:int = 6;

		private static var albumPage:int=1;
		private static var draftPage:int=1;

		public static function getAlbumList():void
		{
			if(alubmUpdating||albumEnd)
				return;
			alubmUpdating=true;
			API.instance.getAlbumList(albumPage,PAGESIZE,albumCB);
		}

		private static var itemGAP:Number = 310;

//        public static var albumListHeight:Number;
//        public static var draftListHeight:Number;

		private static var albumEnd:Boolean;
		private static var draftEnd:Boolean;

		private static function albumCB(o:ResultVO):void
		{
			alubmUpdating=false;
			if(o.status)
			{
				var arr:Array = o.results as Array;
				if(arr&&arr.length)
				{
					if(arr.length!=PAGESIZE)
						albumEnd=true;
					albumPage++;
					albumList.addAll(new ArrayCollection(arr));
				}else
				{
					albumEnd=true;
				}

//				albumListHeight=Math.ceil(albumList.length / 3) * itemGAP;
			}else
			{

			}
		}

		public static function getDraftList():void
		{
			if(draftUpdating||draftEnd)
				return;
			draftUpdating=true;
			API.instance.getDraftList(draftPage,PAGESIZE,draftCB);
		}

		private static function draftCB(o:ResultVO):void
		{
			draftUpdating=false;
			if(o.status)
			{
				var arr:Array = o.results as Array;
				if(arr&&arr.length)
				{
					if(arr.length!=PAGESIZE)
						draftEnd=true;
					draftPage++;
					draftList.addAll(new ArrayCollection(arr));
				}else
					draftEnd=true;

//				draftListHeight=Math.ceil(draftList.length / 3) * itemGAP;
			}else
			{

			}
		}

		private static var alubmUpdating:Boolean;
		private static var draftUpdating:Boolean;

		public static function refresh(album:Boolean,draft:Boolean):void
		{
			if(album)
			{
				albumEnd=false;
				alubmUpdating=false;
				albumList=new ArrayCollection();
				albumPage=1;
				getAlbumList();
			}

			if(draft)
			{
				draftEnd=false;
				draftUpdating=false;
				draftList=new ArrayCollection();
				draftPage=1;
				getDraftList();
			}
		}

		[Bindable]
		public static var albumList:ArrayCollection = new ArrayCollection();

		[Bindable]
		public static var draftList:ArrayCollection  = new ArrayCollection();
	}
}

